package com.danco.sn.entitiesfactory;

import java.util.HashMap;

import com.danco.sn.apientities.ICommunity;
import com.danco.sn.apientities.ICommunityMessage;
import com.danco.sn.apientities.IConversation;
import com.danco.sn.apientities.IFriend;
import com.danco.sn.apientities.IMessage;
import com.danco.sn.apientities.INews;
import com.danco.sn.apientities.IUser;

import java.lang.reflect.Type;

@SuppressWarnings("rawtypes")
public class EntitiesFactory {
	private String NOT_FOUND = "This type not found";
	private String NOT_OBJECT = "This object not create";	
	private HashMap<Class, String> classMap = new HashMap<Class, String>();
	private static EntitiesFactory entitiesFactory;

	private EntitiesFactory() {	
		classMap.put(ICommunity.class, "com.danco.sn.entities.Community");
		classMap.put(ICommunityMessage.class, "com.danco.sn.entities.CommunityMessage");
		classMap.put(IConversation.class, "com.danco.sn.entities.Conversation");	
		classMap.put(IUser.class, "com.danco.sn.entities.User");
		classMap.put(IFriend.class, "com.danco.sn.entities.Friend");
		classMap.put(IMessage.class, "com.danco.sn.entities.Message");
		classMap.put(INews.class, "com.danco.sn.entities.News");
	}

	public static EntitiesFactory getInstance() {
		if (entitiesFactory == null) {
			entitiesFactory = new EntitiesFactory();
		}
		return entitiesFactory;
	}

	public Object getObject(Type cInterface) {
		try {
			String className = classMap.get(cInterface);
			Class currentClass = Class.forName(className);
			return currentClass.newInstance();			
		}catch (ClassNotFoundException clex) {
			clex.printStackTrace();
			throw new RuntimeException(NOT_FOUND);
		} catch (Exception e) {
			throw new RuntimeException(NOT_OBJECT);
		}
	}

}
